package com.achao.backstagemanage.controller;

import com.achao.backstagemanage.service.MusicManagerService;
import com.achao.backstagemanage.utils.BackstageManageUtils;
import com.achao.basecommon.annotation.JwtCheckValidation;
import com.achao.basecommon.constants.CommonConstants;
import com.achao.basecommon.entity.MusicManagerDO;
import com.achao.basecommon.utils.CommonUtils;
import com.achao.basecommon.utils.JwtUtil;
import com.achao.basecommon.utils.R;
import com.achao.basecommon.utils.RedisUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：后台管理者相关类-控制层
 *
 * @Author：yangchao
 * @CreateDate：2023/2/1 21:25
 */
@RestController
@RequestMapping("/achao/music/after/manager/")
public class MusicManagerController {

    @Autowired
    private MusicManagerService musicManagerService;

    @Autowired
    private RedisUtils redisUtils;

    //这个是测试案例
    @PostMapping("queryManager")
    public Object queryManager(){

        return musicManagerService.queryManagerList();
    }

    /**
     *  描述：管理员-注册
     *      @Param：jsonObject
     *      @Returen：R
     */
    @PostMapping("registerManage")
    @JwtCheckValidation
    public Object registerManage(@RequestBody JSONObject jsonObject){
        CommonUtils.verifyThatTheParamsIsNull(
                jsonObject,
                "managerName",
                "gender","phoneNo","createTime"
        );
        MusicManagerDO musicManagerDO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), MusicManagerDO.class);
        //判断用户是否已注册，已注册则提示失败
        if(null != musicManagerService.queryRegisterManager(musicManagerDO)){
            return R.failResult("该管理员用户已注册!!!");
        }
        musicManagerService.insertRegistryMangage(musicManagerDO);
        return R.successResult("管理注册成功");
    }

    /**
     *  描述：管理员-登录
     *      @Param：jsonObject
     *      @Returen：R
     */
    @PostMapping("loginManage")
    //@JwtCheckValidation
    public Object loginManage(@RequestBody JSONObject jsonObject){
        if(StringUtils.isEmpty(jsonObject.getString("managerName"))){
            return R.failResult("登录用户名不可为空!!!");
        }
        String managerName = jsonObject.getString("managerName");
        //redis缓存路径
        String cachePath = BackstageManageUtils.getCacheFilePath("manage", managerName);

        //缓存查找用户信息
        if(redisUtils.hasKey(cachePath)){
            redisUtils.setCacheObject(cachePath,1,1L, TimeUnit.DAYS);
            return R.successResult(JwtUtil.generateJwt(JwtUtil.createClaims(managerName)),"已登录，无需重复登录！");
        }
        //缓存找不到去数据库找
        MusicManagerDO musicManagerDO = new MusicManagerDO();
        musicManagerDO.setManagerName(managerName);
        if(null == musicManagerService.queryRegisterManager(musicManagerDO)){
            return R.failResult("用户不存在!!!");
        }
        redisUtils.setCacheObject(cachePath,1,1L, TimeUnit.DAYS);
        //生成jwt令牌并返回
        return R.successResult(JwtUtil.generateJwt(JwtUtil.createClaims(managerName)),"登录成功");
    }

    /**
     *  描述：查询所有管理员
     *      @Param：null
     *      @Returen：R
     */
    @PostMapping("getAllManage")
    public Object queryAllManagerList(){
        //设置分页参数
        CommonUtils.setPageHelperParam(CommonConstants.PAGE_NUM_DEFAULT,CommonConstants.PAGE_SIZE_DEFAULT);
        List<MusicManagerDO> musicManagerDOS = musicManagerService.queryAllManagerList();
        return CommonUtils.getPageInfo(Collections.singletonList(musicManagerDOS));
    }

    /**
     *  描述：根据管理员-id查询信息
     *      @Param：ID
     *      @Returen：MusicManagerDO
     */
    @PostMapping("getManageForId")
    public Object queryManagerForId(@RequestBody JSONObject jsonObject){
        return musicManagerService.queryManagerForId(Integer.parseInt(jsonObject.getString("id")));
    }

    /**
     *  描述：根据ID-编辑管理员信息
     *      @Param：jsonObject
     *      @Returen：void
     */
    @PostMapping("updateManageForId")
    public Object updateManagerInfo(@RequestBody JSONObject jsonObject){
        CommonUtils.verifyThatTheParamsIsNull(
                jsonObject,
                "managerName",
                "gender","phoneNo","createTime"
        );
        MusicManagerDO musicManagerDO = JSONObject.parseObject(JSON.toJSONString(jsonObject), MusicManagerDO.class);
        Integer updateCount = musicManagerService.updateManagerInfo(musicManagerDO);
        if(1 > updateCount){
            CommonUtils.throwMusicConmonException("更新管理员信息-失败");
        }
        return R.successResult("管理员信息更新成功:" + updateCount + "条");
    }

    /**
     *  描述：根据ID-删除管理员信息
     *      @Param：jsonObject
     *      @Returen：Object
     */
    @PostMapping("delManagerForId")
    public Object delManagerInfo(@RequestBody JSONObject jsonObject){
        musicManagerService.delManagerInfo(Integer.parseInt(jsonObject.getString("id")));
        return R.successResult("删除管理人员信息成功");
    }

    /**
     *  @Description：新增管理员
     *  @Param：MusicManagerDO
     *  @Returen：R
     */
    @PostMapping("saveManagerInfo")
    public R saveManagerInfo(@RequestBody JSONObject jsonObject){
        CommonUtils.verifyThatTheParamsIsNull(
                jsonObject,
                "managerName",
                "gender","phoneNo","createTime"
        );
        MusicManagerDO musicManagerDO = JSONObject.parseObject(JSON.toJSONString(jsonObject), MusicManagerDO.class);
        musicManagerService.saveManagerInfo(musicManagerDO);
        return R.successResult("成功添加管理员");
    }

}
